package com.yx.study.leet.questionList.questionTwo;

import org.junit.Test;

/**
 * 69. x 的平方根
 * https://leetcode.cn/problems/sqrtx/description/
 */
public class Solution69 {
    public int mySqrt(int x) {
        // 二分查找 寻找y^2<=x的最大值
        int l = 0, r = x, ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long) mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

    @Test
    public void test() {
        int x = 3;
        System.out.println(mySqrt(x));
    }
}

